package com.aoxin.controller.course.systemCourse;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.aoxin.model.course.AloneCourse;
import com.aoxin.service.inter.course.systemCourse.AloneCourseService;
import com.aoxin.util.AjaxRes;
import com.aoxin.util.Const;
import com.aoxin.util.enumerate.OperateType;
import com.aoxin.util.log.LogInfo;

@RestController
@RequestMapping("/web/system/")
public class AloneCourseController {
    private static Logger log = LoggerFactory.getLogger(AloneCourseController.class);
    @Autowired
    private AloneCourseService aloneCourseService;

    /**
     * 新增单独课程
     * @param aloneCourse
     * @param res
     * @return
     */
    @LogInfo(content = "新增单独课程",operateType = OperateType.INSERT)
    @RequestMapping("insertAloneCourse")
    public AjaxRes insertAloneCourse(AloneCourse aloneCourse, AjaxRes res) {
        log.info("新增:" + aloneCourse.toString());
        try {
        	aloneCourse.setDescription(aloneCourse.getDescription().replaceAll("$aoxin", "#"));
            aloneCourseService.insertAloneCourse(aloneCourse);
            Integer id = aloneCourse.getCourseId();
            res.setSucceed(String.valueOf(id));
        }  catch (Exception e) {
            e.printStackTrace();
            res.setFailMsg(e.getMessage());
        }
        return res;
    }

    /**
     * 查询单独课程
     * @param aloneCourse
     * @param res
     * @return
     */
    @LogInfo(content = "查询单独课程",operateType = OperateType.QUERY)
    @GetMapping(value = "getAloneCourse")
    public AjaxRes getAloneCourse(AloneCourse aloneCourse,Integer state, AjaxRes res) {
        try {
            List<AloneCourse> aloneCourses = aloneCourseService.getAloneCourse(aloneCourse,state);
            res.setSucceed(aloneCourses);
        } catch (Exception e) {
            res.setFailMsg(e.getMessage());
            e.printStackTrace();
        }
        return res;
    }

    
    /**
     * 修改单独的课程
     * @param aloneCourse
     * @return
     */
    @LogInfo(content = "单独课程",operateType = OperateType.UPDATE)
    @RequestMapping("updateAloneCourse")
    public AjaxRes updateAloneCourse(AloneCourse aloneCourse, AjaxRes res) {
        try {
        	aloneCourse.setDescription(aloneCourse.getDescription().replaceAll("$aoxin", "#"));
            Integer integer = aloneCourseService.updateAloneCourse(aloneCourse);
            res.setSucceed(integer);
        } catch (Exception e) {
            res.setFailMsg(e.getMessage());
            e.printStackTrace();
        }
        return res;
    }
    
    @LogInfo(content = "修改课程状态",operateType = OperateType.UPDATE)
    @RequestMapping("updateAloneCourseState")
    public AjaxRes updateAloneCourseState(@RequestParam("courseIds") List<Integer> courseIds,Integer state, AjaxRes res) {
        try {
            this.aloneCourseService.updateAloneCourseState(courseIds,state);
            res.setSucceed(Const.SUCCESS);
        } catch (Exception e) {
            res.setFailMsg(e.getMessage());
            e.printStackTrace();
        }
        return res;
    }
    /**
     * 查询课程包下课程
     * @param coursePageId
     * @param res
     * @return
     */
    @LogInfo(content = "查询课程包下课程",operateType = OperateType.QUERY)
    @GetMapping(value = "getAlonCourseToCoursePageId")
    public AjaxRes getAlonCourseToCoursePageId(Integer coursePageId, AjaxRes res) {
        try {
            List<AloneCourse> aloneCoursesList = this.aloneCourseService.getAlonCourseToCoursePageId(coursePageId);
            res.setSucceed(aloneCoursesList);
        } catch (Exception e) {
            res.setFailMsg(e.getMessage());
            e.printStackTrace();
        }
        return res;
    }
    
}
